package com.yifi.system.controller;

import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseController;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.system.domain.MisUser;

import cn.hutool.core.util.ReUtil;

@Path(value="/system",viewPath = "/")
public class SystemController extends BaseController {

	//个人中心
	public void profile()
	{
		MisUser misUser=ToolSession.getUser(this);
		set("user", misUser);
		
		set("deptname",Db.queryStr("select cname from mis_org where ccode='"+misUser.getCdept()+"'"));
		render("system/profile.html");
	}
	
	//修改密码
	public void resetPwd()
	{
		MisUser misUser=ToolSession.getUser(this);
		set("user", misUser);
		render("system/resetPwd.html");
	}
	
	public void savePass()
	{
		String ccode=getPara("ccode","");
		String oldPassword=getPara("oldPassword","");
		String newPassword=getPara("newPassword","");

    	//String regex="^(?![0-9]+$)(?![a-zA-Z]+$)(?![_!@#$%^&*]+$)[0-9A-Za-z_!@#$%^&*]{8,16}$";
		
    	String regex="^(?=.*?[a-z])(?=.*?[0-9])(?=.*?[_\\-@&=])[a-z0-9_\\-@&=]{8,16}$";  //必须包含数字字母特殊符号
    	
		if(!ReUtil.isMatch(regex,newPassword))
		{
			renderJson(error("密码必须包含数字字母特殊字符8-16位！"));return;
		}
		
		String confirmPassword=getPara("confirmPassword","");
		String sql="select * from mis_user where ccode='"+ccode+"' and cpass='"+oldPassword+"'";
		Record record=Db.findFirst(sql);
		if(record==null)
		{
			renderJson(error("历史密码填写错误！"));return;
		}
		else
		{
			if(newPassword.equals(confirmPassword))
			{
				record.set("cpass", newPassword);
				
				if(DbUtils.save("mis_user", "ccode", record, true))
				{
					renderJson(success("保存成功！"));return;
				}
				else
				{
					renderJson(error("保存失败！"));return;
				}
			}
			else
			{
				renderJson(error("密码请填写一致！"));return;
			}
		}
		
	}
	public void saveUser()
	{
		String ccode=getPara("ccode","");
		String cname=getPara("cname","");
		String cphone=getPara("cphone","");
		String cemail=getPara("cemail","");
		String sql="select * from mis_user where ccode='"+ccode+"' ";
		Record record=Db.findFirst(sql);
		if(record==null)
		{
			renderJson(error("保存错误！"));
			return;
		}
		record.set("cname", cname);
		record.set("cphone", cphone);
		record.set("cemail", cemail);
		
		if(DbUtils.save("mis_user", "ccode", record, true))
		{
			renderJson(success("保存成功！"));
		}
		else
		{
			renderJson(error("保存失败！"));
		}
		
	}
	
	//切换后台颜色样式
	public void switchSkin()
	{
		render("system/skin.html");
	}
}
